// pages/step/step.js
const app = getApp();
import WxValidate from "../../utils/WxValidate.js";
const wh = require("../../utils/wh.js");
var Validate = ""
Page({

  /**
   * 页面的初始数据
   */
  data: {
    justImg1: '../../images/s2.png',
    backImg1: '../../images/s3.png',
    dataImg: '../../images/s7.png',
    dataImg1: '../../images/s8.png',
    dataImg2: '../../images/s7.png',
    dataImg3: '../../images/s8.png',
    dataImg4: '../../images/s8.png',
    risk_letter: '',
    auth_letter: '',
    face_sign_letter1: '',
    driver_card_positive_photo: '',
    driver_card_negative_photo: '',
    financing_apply_letter: '',
    auth_search_letter: '',
    lid: '',
    loan_id:'',
    download_files: [],
    else_apply_material: '' // 其他
  },
  // 身份证正面
  uploadPic: function(e) {
    var that = this,
      param_name = e.currentTarget.dataset.param
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths,
          api = '/api/madetoorder.loan.index/add_user_material',
          filePath = tempFilePaths[0];
        let data = wh.Wxupload(api, filePath, param_name).then(function(res) {
          switch (param_name) {
            case 'driver_card_positive_photo':
              that.setData({
                driver_card_positive_photo: res.path,
                justImg1: filePath
              });
              break;
            case 'driver_card_negative_photo':
              that.setData({
                driver_card_negative_photo: res.path,
                backImg1: filePath
              });
              break;
            case 'auth_letter':
              that.setData({
                auth_letter: res.path,
                dataImg1: filePath
              });
              break;
            case 'face_sign_letter1':
              that.setData({
                face_sign_letter1: res.path,
                dataImg2: filePath
              });
              break;
            case 'financing_apply_letter':
              that.setData({
                financing_apply_letter: res.path,
                dataImg3: filePath
              });
              break;
            case 'else_apply_material':
              that.setData({
                else_apply_material: res.path,
                dataImg4: filePath
              });
              break;
          }
        });
      }
    })
  },
  goPerson: function () {
    let postUrl = '/api/madetoorder.loan.index/stage_one_apply',
      that = this,
      loan_id = that.data.loan_id,
      params = {
        lu_id: that.data.lid,
        face_sign_letter1: that.data.face_sign_letter1,
        driver_card_positive_photo: that.data.driver_card_positive_photo,
        driver_card_negative_photo: that.data.driver_card_negative_photo,
        financing_apply_letter: that.data.financing_apply_letter,
        auth_letter: that.data.auth_letter,
        else_apply_material: that.data.else_apply_material
      };

    wh.Post(postUrl, params, function(res) {
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 2000,
        complete: function() {
          setTimeout(function() {
            wx.switchTab({
              url: '../person/person',
            })
          }, 2000);
        }
      });
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      lid: options.lu_id,
      loan_id: options.loan_id
    })
    this.data.currentTab = options.currentTab;
    this.get_download_files(options.loan_id);
  },
  /**
   * 获取附件列表
   */
  get_download_files: function(id) {
    let postUrl = '/api/madetoorder.loan.index/download_files',
      postData = {loan_id: id},
      that = this;

    wh.Post(postUrl, postData, function(res) {
      that.data.download_files = res;
    });
  },
  /**
   * 下载附件
   */
  to_download_files: function() {
    wx.showLoading({
      title: '下载中...',
    });
    let data = this.data.download_files;
    let len = data.length;
    for (let i = 0; i < data.length; i++) {
      wx.downloadFile({
        url: data[i].attachment_url, //仅为示例，并非真实的资源
        success: function (res) {
          // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
          if (res.statusCode === 200) {
            wx.playVoice({
              filePath: res.tempFilePath
            })
          } else {
            wx.showModal({
              title: '提示',
              content: '下载失败',
            })
            return;
          }
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success:function (data) {
              if (i == len-1) {
                wx.hideLoading();
              }
            },
          })
        }
      })
    }
  },
  // 身份证表单提交
  formSubmit: function(e) {
    if (this.data.driver_card_positive_photo == '') {
      wx.showToast({
        title: '请选择驾驶证主页',
        icon: 'none'
      });
      return false
    }
    if (this.data.driver_card_negative_photo == '') {
      wx.showToast({
        title: '请选择驾驶证副页',
        icon: 'none'
      });
      return false
    }
    if (this.data.auth_letter == '') {
      wx.showToast({
        title: '请选择征信授权书',
        icon: 'none'
      });
      return false
    }
    if (this.data.face_sign_letter1 == '') {
      wx.showToast({
        title: '请选择征信面签照',
        icon: 'none'
      });
      return false
    }
    if (this.data.financing_apply_letter == '') {
      wx.showToast({
        title: '请选择申请表',
        icon: 'none'
      });
      return false
    }
    this.goPerson()
  },
  

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setData({
      currentTab_back: this.data.currentTab
    });
  }
})